package spring.yongongsystem.controller.worker;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import spring.yongongsystem.POJO.dto.MerchantDTO;
import spring.yongongsystem.POJO.dto.WorkerDTO;
import spring.yongongsystem.POJO.entity.Merchant;
import spring.yongongsystem.POJO.entity.Worker;
import spring.yongongsystem.POJO.vo.MerchantLoginVO;
import spring.yongongsystem.POJO.vo.WorkerLoginVO;
import spring.yongongsystem.constant.JwtClaimsConstant;
import spring.yongongsystem.properties.JwtProperties;
import spring.yongongsystem.result.Result;
import spring.yongongsystem.service.MerchantService;
import spring.yongongsystem.service.WorkerService;
import spring.yongongsystem.utils.JwtUtil;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController("workerWorkerController")
@RequestMapping("/worker/worker")
@Api(tags = "用工个人相关接口")
@Slf4j
public class WorkerController {
    String key = "worker_token";
    @Resource
    private RedisTemplate redisTemplate;
    @Autowired
    private WorkerService workerService;
    @Autowired
    private JwtProperties jwtProperties;
    /**
     * 登录
     *
     * @param workerDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "用工登陆")
    public Result<WorkerLoginVO> login(@RequestBody WorkerDTO workerDTO) {
        log.info("用工登录：{}",workerDTO);

        Worker worker = workerService.login(workerDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, worker.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        WorkerLoginVO workerLoginVO = WorkerLoginVO.builder()
                .id(worker.getId())
                .name(worker.getName())
                .userName(worker.getUsername())
                .phone(worker.getPhone())
                .token(token)
                .build();
        redisTemplate.opsForValue().set(key,token);
        return Result.success(workerLoginVO);
    }

    /**
     * 新增用工
     * @param workerDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增用工")
    public Result save(@RequestBody WorkerDTO workerDTO){
        log.info("新增用工：{}",workerDTO);
        workerService.save(workerDTO);
        return Result.success();
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 编辑用工信息
     * @param workerDTO
     * @return
     */
    @PutMapping
    @ApiOperation("编辑用工信息")
    public Result update(@RequestBody WorkerDTO workerDTO){
        log.info("编辑用工信息：{}", workerDTO);
        workerService.update(workerDTO);
        return Result.success();
    }
}
